package com.tedu.demo;


//模拟售票
public class Test04_Exercise01 {
    public static void main(String[] args) {
        Thread thread1 = new MyThread1();
        thread1.setName("窗口一");
        Thread thread2 = new MyThread1();
        thread2.setName("窗口二");
        Thread thread3 = new MyThread1();
        thread3.setName("窗口三");
        Thread thread4 = new MyThread1();
        thread4.setName("窗口四");
        //new了四次 所以导致成了400张票
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}
class MyThread1 extends Thread{
    //static静态变量是全局唯一变量
    //当含有静态资源时，只能对代码块加锁，锁对象只能是  类名.class
    static int tickes = 100;
    @Override
    public void run() {
        while(true){
            synchronized (MyThread1.class){
                if (tickes>0){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("票号" + tickes-- + "售票口为：" + super.getName());
                }else{
                    break;
                }
            }
        }
    }
}